// Copyright (c) 2025, 大连理工大学 (Dalian University of Technology)
//
// Licensed under the Mulan PSL v2.
// You can use this file according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of the License at
//
//     http://license.coscl.org.cn/MulanPSL2
//
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. SEE THE MULAN PSL v2
// FOR MORE DETAILS.

#include <gtest/gtest.h>

#include "DBManager/DBServiceFactory.h"
#include "DBManager/IService/Input/Subcase/ISubcaseService.h"
#include "DBManager/IService/Input/Table/ITABDMP1Service.h"
#include "DBManager/UnitTest/HDF5/TestDB.h"
using namespace SG::DataStructure::FEM;
using namespace SG::DBManager;

TEST (I_TableTest, TABDMP1Test)
{
    DB::ServiceFactory->ClearDatabase ();

    auto service = DB::ServiceFactory->get<ITABDMP1Service> ();

    SG::DataStructure::FEM::TABDMP1Data data;
    data.m_id = 1;
    ASSERT_EQ (service->Add (data), SG::DataStructure::Common::Status::STATUS_Success);

    SG::DataStructure::FEM::TABDMP1Data _OUT outData;
    ASSERT_EQ (service->FindById (1, outData), SG::DataStructure::Common::Status::STATUS_Success);
    ASSERT_EQ (outData.m_id, 1);

    ASSERT_EQ (service->FindByIndex (0, outData), SG::DataStructure::Common::Status::STATUS_Success);
    ASSERT_EQ (outData.m_id, 1);

    ASSERT_EQ (service->Size (), 1);
    ASSERT_EQ (service->Count (1), 1);
}

#include "DBManager/IService/Input/Table/ITABLED1Service.h"
TEST (I_TableTest, TABLED1Test)
{
    DB::ServiceFactory->ClearDatabase ();

    auto service = DB::ServiceFactory->get<ITABLED1Service> ();

    SG::DataStructure::FEM::TABLED1Data data;
    data.m_id = 1;
    ASSERT_EQ (service->Add (data), SG::DataStructure::Common::Status::STATUS_Success);

    SG::DataStructure::FEM::TABLED1Data _OUT outData;
    ASSERT_EQ (service->FindById (1, outData), SG::DataStructure::Common::Status::STATUS_Success);
    ASSERT_EQ (outData.m_id, 1);

    ASSERT_EQ (service->FindByIndex (0, outData), SG::DataStructure::Common::Status::STATUS_Success);
    ASSERT_EQ (outData.m_id, 1);

    ASSERT_EQ (service->Size (), 1);
    ASSERT_EQ (service->Count (1), 1);
}

#include "DBManager/IService/Input/Table/ITABLED2Service.h"
TEST (I_TableTest, TABLED2Test)
{
    DB::ServiceFactory->ClearDatabase ();

    auto service = DB::ServiceFactory->get<ITABLED2Service> ();

    SG::DataStructure::FEM::TABLED2Data data;
    data.m_id = 1;
    ASSERT_EQ (service->Add (data), SG::DataStructure::Common::Status::STATUS_Success);

    SG::DataStructure::FEM::TABLED2Data _OUT outData;
    ASSERT_EQ (service->FindById (1, outData), SG::DataStructure::Common::Status::STATUS_Success);
    ASSERT_EQ (outData.m_id, 1);

    ASSERT_EQ (service->FindByIndex (0, outData), SG::DataStructure::Common::Status::STATUS_Success);
    ASSERT_EQ (outData.m_id, 1);

    ASSERT_EQ (service->Size (), 1);
    ASSERT_EQ (service->Count (1), 1);
}

#include "DBManager/IService/Input/Table/ITABLED3Service.h"
TEST (I_TableTest, TABLED3Test)
{
    DB::ServiceFactory->ClearDatabase ();

    auto service = DB::ServiceFactory->get<ITABLED3Service> ();

    SG::DataStructure::FEM::TABLED3Data data;
    data.m_id = 1;
    ASSERT_EQ (service->Add (data), SG::DataStructure::Common::Status::STATUS_Success);

    SG::DataStructure::FEM::TABLED3Data _OUT outData;
    ASSERT_EQ (service->FindById (1, outData), SG::DataStructure::Common::Status::STATUS_Success);
    ASSERT_EQ (outData.m_id, 1);

    ASSERT_EQ (service->FindByIndex (0, outData), SG::DataStructure::Common::Status::STATUS_Success);
    ASSERT_EQ (outData.m_id, 1);

    ASSERT_EQ (service->Size (), 1);
    ASSERT_EQ (service->Count (1), 1);
}

#include "DBManager/IService/Input/Table/ITABLED4Service.h"
TEST (I_TableTest, TABLED4Test)
{
    DB::ServiceFactory->ClearDatabase ();

    auto service = DB::ServiceFactory->get<ITABLED4Service> ();

    SG::DataStructure::FEM::TABLED4Data data;
    data.m_id = 1;
    ASSERT_EQ (service->Add (data), SG::DataStructure::Common::Status::STATUS_Success);

    SG::DataStructure::FEM::TABLED4Data _OUT outData;
    ASSERT_EQ (service->FindById (1, outData), SG::DataStructure::Common::Status::STATUS_Success);
    ASSERT_EQ (outData.m_id, 1);

    ASSERT_EQ (service->FindByIndex (0, outData), SG::DataStructure::Common::Status::STATUS_Success);
    ASSERT_EQ (outData.m_id, 1);

    ASSERT_EQ (service->Size (), 1);
    ASSERT_EQ (service->Count (1), 1);
}

#include "DBManager/IService/Input/Table/ITABLED5Service.h"
TEST (I_TableTest, TABLED5Test)
{
    DB::ServiceFactory->ClearDatabase ();

    auto service = DB::ServiceFactory->get<ITABLED5Service> ();

    SG::DataStructure::FEM::TABLED5Data data;
    data.m_id = 1;
    ASSERT_EQ (service->Add (data), SG::DataStructure::Common::Status::STATUS_Success);

    SG::DataStructure::FEM::TABLED5Data _OUT outData;
    ASSERT_EQ (service->FindById (1, outData), SG::DataStructure::Common::Status::STATUS_Success);
    ASSERT_EQ (outData.m_id, 1);

    ASSERT_EQ (service->FindByIndex (0, outData), SG::DataStructure::Common::Status::STATUS_Success);
    ASSERT_EQ (outData.m_id, 1);

    ASSERT_EQ (service->Size (), 1);
    ASSERT_EQ (service->Count (1), 1);
}

#include "DBManager/IService/Input/Table/ITABRND1Service.h"
TEST (I_TableTest, TABRND1Test)
{
    // DB::ServiceFactory->ClearDatabase ();

    auto service = DB::ServiceFactory->get<ITABRND1Service> ();

    SG::DataStructure::Common::Id id = 7083764782846131555;

    SG::DataStructure::FEM::TABRND1Data data;
    data.m_id = id;
    ASSERT_EQ (service->Add (data), SG::DataStructure::Common::Status::STATUS_Success);

    SG::DataStructure::FEM::TABRND1Data _OUT outData;
    ASSERT_EQ (service->FindById (id, outData), SG::DataStructure::Common::Status::STATUS_Success);
    ASSERT_EQ (outData.m_id, id);

    ASSERT_EQ (service->FindByIndex (0, outData), SG::DataStructure::Common::Status::STATUS_Success);
    ASSERT_EQ (outData.m_id, id);

    ASSERT_EQ (service->Size (), 1);
    ASSERT_EQ (service->Count (id), 1);
}
